/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.henryschein.mmvvcc.controller;

import com.henryschein.mmvvcc.dao.UserDao;
import com.henryschein.mmvvcc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author obelog
 */
@Controller
@RequestMapping("/subscr")
public class SubscribeController {
    
    @Autowired
    private UserDao userDao;
    
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView importQuote(ModelMap model) {
        User user = new User();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("sub");
        mav.addObject("user", user);
        mav.addObject("users", userDao.getUsers());
        return mav;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView importQuotePost(@ModelAttribute User user) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("sub");
        userDao.save(user);
        mav.addObject("message", "You " + user.getMail() + " subscribed");
        return mav;
    }
    
}
